<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card
        v-loading="loading"
        element-loading-text="我是来搞笑哦啊"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        class="tree-card"
      >
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-root="true" @add-depts="handleAddDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">

            <!-- <p>{{ data }} </p> -->
            <tree-tools :node-data="data" @del-depts="getDepartments" @add-depts="handleAddDepts" @edit-depts="handleEditDepts" />
          </template>
        </el-tree>
      </el-card>
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :node-data="nodeData" :depts-list="deptsList" @close-dialog="handleClose" @add-depts="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept' // 引入新增部门组件
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      deptsList: [],
      company: { name: '', manager: '负责人' },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      nodeData: {}

    }
  },

  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      await new Promise((reslove) => {
        setTimeout(() => {
          reslove()
        }, 2000)
      })

      const { data: { depts, companyName }} = await reqGetDepartments()
      this.company.name = companyName
      this.company.id = ''
      const treeData = tranListToTreeData(depts, '') // 需要将其转换成树形结构
      // console.log(treeData)

      this.departs = treeData
      this.deptsList = depts // 列表式数据
      this.loading = false
    },
    handleClose(flag) {
      this.showDialog = flag
    },
    handleAddDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
      this.$refs.addDept.getEmployeeSimple()
    },
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$refs.addDept.getDepartDetail(nodeData.id)
      this.$refs.addDept.getEmployeeSimple()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
